<template>
  <div style="margin-top: 30px">
    <el-tabs v-model="activeOrgName" type="border-card" @tab-click="handleCheckTabs">
			<el-tab-pane v-for="(item, index) in orgList" :key="index" :label="item.label" :name="item.value">
				<template v-slot:label>
					<div class="tab-label-box">{{ item.label }}</div>
				</template>
			</el-tab-pane>
      <ReportDetail v-if="!loading" :creditCode="nowRowData.creditCode" v-bind="$attrs" :nowRowData="rowData" @refresh="refresh" :organizationId="activeOrgItem?.orgId" :navTagList="tabsList"/>
		</el-tabs>
  </div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod/index'
import NavTabs from '@/views/instOperate/components/NavTabs/index.vue'
import ReportDetail from './ReportDetail/index.vue'


const props = defineProps<{
  nowRowData:any
}>()

let emit = defineEmits(['refresh'])
const refresh = () =>{
  emit('refresh')
}

const loading = ref(true)
const activeOrgName = ref(0)
const tabsList = [
  { label: '申请表', value: 2 },
  // { label: '床位审核表', value: 4 },
  // { label: '附件材料', value: 3 },
]
const rowData = ref({})
const orgList = ref([])
const activeOrgItem = computed(()=> {
   return orgList.value.find((item) => item.value === activeOrgName.value)
})

const handleCheckTabs = (pane)=> {
  const item = orgList.value.find((item) => item.value === pane.paneName)
  getApplyInfo(item.orgId)
}
const getRandomValue = () => {
  const idsArr = props.nowRowData.ids?.split(',')||[]
  const dateArr = props.nowRowData.belongDateLabels?.split(',')||[]
  if(idsArr.length>0){
    orgList.value = idsArr.map((id,index) => {
    let tempObj = {
      label: dateArr[index],
      orgId: id,
      value:index
    }
    if(index ==0) {
      getApplyInfo(id)
    }
    return tempObj
  })
  }else{
    getApplyInfo(props.nowRowData.applyId)
  }
 
}

const getApplyInfo = (orgId?:string)=> {
  loading.value = true 
  apiMod.view_ofsSubsidyApply_view(orgId||activeOrgItem.value.orgId).then(res=> {
    rowData.value = {
      ...res.data,
      // belongDate:activeOrgItem.value.label.replaceAll('月','').replaceAll('年','-'),
      belongDate:props.nowRowData.belongDate
    }
    setTimeout(() => {
      loading.value = false
    }, 100);
  }).catch(()=> {
    loading.value = false
  })
}
watch(()=>props.nowRowData,(newVal)=>{
  if(newVal&&newVal.applyId){
    getRandomValue()
  }
},{deep:true,immediate:true})


</script>

<style scoped lang="scss">

</style>